package com.zhangmeng.online.exam.ui.layouts;

import com.zhangmeng.online.exam.ui.admin.IndexPage;
import com.zhangmeng.online.exam.ui.utils.ApiUtils;
import com.zhangmeng.online.exam.ui.components.DynamicTableComponent;
import com.zhangmeng.online.exam.ui.components.callBack.DynamicTableComponentCallBackImpl;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

import java.util.List;


/**
 * @author zm
 * @date 2025/3/5 9:42
 * @version: 1.0
 */
public class SideMenu extends VBox {

    public static class SideMenuData {
        private String text;
        private String url;

        public SideMenuData(String text, String url) {
            this.text = text;
            this.url = url;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }

    public static class SideMenuItem {

        private String text;
        private List<SideMenuData> sideMenuData;

        public SideMenuItem(String text, List<SideMenuData> sideMenuData) {
            this.text = text;
            this.sideMenuData = sideMenuData;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public List<SideMenuData> getSideMenuData() {
            return sideMenuData;
        }

        public void setSideMenuData(List<SideMenuData> sideMenuData) {
            this.sideMenuData = sideMenuData;
        }
    }


    public SideMenu(List<SideMenuItem> menuItems) {

        this.setSpacing(10);
        this.setPrefWidth(200);
//        this.setStyle("-fx-background-color: #2c3e50;");

        Accordion accordion = new Accordion();

        for (SideMenuItem item : menuItems) {

            TitledPane tp = new TitledPane();
            tp.setText(item.getText());

            VBox vbox = new VBox();
            vbox.setPadding(javafx.geometry.Insets.EMPTY);
            vbox.setAlignment(javafx.geometry.Pos.CENTER);

            for (SideMenuData data : item.getSideMenuData()) {

                HBox hbox = new HBox();
                hbox.setSpacing(10);
                hbox.setAlignment(javafx.geometry.Pos.CENTER);
                Label label = new Label(data.getText());
                hbox.getChildren().add(label);
                vbox.getChildren().add(hbox);
                hbox.hoverProperty().addListener(new ChangeListener<Boolean>() {
                    @Override
                    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                        if (newValue) {
                            hbox.setStyle("-fx-background-color: #b3f0ee;");
                        } else {
                            hbox.setStyle("-fx-background-color: null;");
                        }
                    }
                });

                hbox.setOnMouseClicked(event -> {
                    //System.out.println("hbox.setOnMouseClicked====>" + data.getUrl());
                    switch (data.getUrl()) {
                        case "/user/list" -> init_table(hbox, ApiUtils.getUserList());
                        case "/role/list" -> init_table(hbox, ApiUtils.getRoleList());
                        case "/permission/list" -> init_table(hbox, ApiUtils.getPermissionList());
                        case "/question/list" -> init_table(hbox, ApiUtils.getQuestionList());
                        case "/subject/list" -> init_table(hbox, ApiUtils.getSubjectList());
                        case "/option/list" -> init_table(hbox, ApiUtils.getQuestionOptionList());
                        case "/answer/list" -> init_table(hbox, ApiUtils.getAnswerList());
                        case "/paper/list" -> init_table(hbox, ApiUtils.getExamPaper());
                    }

                });
            }
            tp.setContent(vbox);

            tp.expandedProperty().addListener(new ChangeListener<Boolean>() {
                @Override
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                    //System.out.println("tp.expandedProperty().addListener====>" + newValue);
                }
            });

            accordion.getPanes().add(tp);
        }

        this.getChildren().add(accordion);

        accordion.expandedPaneProperty().addListener(new ChangeListener<TitledPane>() {
            @Override
            public void changed(ObservableValue<? extends TitledPane> observable, TitledPane oldValue, TitledPane newValue) {
                if (newValue == null) {
                    //System.out.println(oldValue.getText() + "=====>折叠");
                    return;
                }
                //System.out.println(newValue.getText() + "=====>展开");
            }
        });

    }


    private void init_table(HBox hbox, ApiUtils.DataView dataView) {
        Scene scene = hbox.getScene();
        IndexPage root = (IndexPage) scene.getRoot();
        dataView.setPageNum(dataView.getPageNum());
        dataView.setPageSize(dataView.getPageSize());
        dataView.setTotal(dataView.getTotal());
        DynamicTableComponent dynamicTableComponent = new DynamicTableComponent(dataView,true);
        dynamicTableComponent.setCallBack(new DynamicTableComponentCallBackImpl(dataView.getDataLoad(),dataView.getDataLoad().getForm()));
        dynamicTableComponent.setPadding(new Insets(15));
        dynamicTableComponent.prefHeightProperty().bind(root.heightProperty().subtract(15));
        root.setCenter(dynamicTableComponent);
    }
}
